<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ include file="/WEB-INF/jsp/include/taglib.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<%@include file="/WEB-INF/jsp/include/head.jsp" %>
<%@include file="/WEB-INF/jsp/include/treeview.jsp" %>
</head>
<body>
<ul id="treeDemo" class="ztree"></ul>
	<script type="text/javascript">
		$(document).ready(function(){
			var setting = {
					  view: {
			               dblClickExpand: true,
			               expandSpeed: 10 //设置树展开的动画速度
			         },
			            //获取json数据
			       async : {  
			             enable : true, 
			             type: "get", 
			             contentType: "application/json",
			             url : "${ctx}/cmsCategory/asyncList.do", // Ajax 获取数据的 URL 地址  
			             autoParam : [ "id", "name" ] //ajax提交的时候，传的是id值
			         },
					data: {
						simpleData: {
							enable: true,
							idKey : "id", // id编号命名   
			                pIdKey : "pId" // 父id编号命名    
						}
					},
				 // 回调函数  
			        callback : {  
			        	beforeClick: beforeClick,
			            onClick : function(event, treeId, treeNode, clickFlag) {  
			            	var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
			            	var nodes = treeObj.getSelectedNodes();
			            	if (nodes.length>0) {
			            		treeObj.expandNode(nodes[0], true, true, true);
			            		zTreeOnExpand(event, treeId, treeNode);
			            	}
			            },
			            //捕获异步加载出现异常错误的事件回调函数 和 成功的回调函数  
			            onAsyncSuccess : function(event, treeId, treeNode, msg){ 
			            	
			            },
			            onExpand: zTreeOnExpand
			        }  
				};
			
				function beforeClick(treeId, treeNode, clickFlag){
					var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
					$.ajax({
						type: "get",
					    url: "${ctx}/cmsCategory/asyncList.do",     //ajax请求地址
					    data : {id:treeNode.id},
					    success: function (data) {
					    	data = data.trim();
					    	if(data.length>2){
						    	treeObj.addNodes(treeNode, data,true);
					    	}
					    },
					    error :function(data){
					    	alert(data);
					    }
					 });
				};
			
				function zTreeOnExpand(event, treeId, treeNode) {
					$.ajax({
						type: "get",
					    url: "${ctx}/cmsCategory/treeTable.do",     //ajax请求地址
					    dataType: "html",
					    data:{"pid":treeNode.id},
					    success: function (data) {
					    	$("#treeTable").html(data);
					    },
					    error :function(data){
					    	alert(data);
					    }
					 });
				};
		   
					/* 		 		
			 		var zNodes = [ {
										"id" : "10",
										"parentId" : "1",
										"name" : "软件介绍", "isParent":true
									}, {
										"id" : "18",
										"parentId" : "1",
										"name" : "友情链接"
									}, {
										"id" : "2",
										"parentId" : "1",
										"name" : "组织机构"
									}, {
										"id" : "24",
										"parentId" : "1",
										"name" : "百度一下"
									}, {
										"id" : "25",
										"parentId" : "1",
										"name" : "全文检索"
									}, {
										"id" : "26",
										"parentId" : "1",
										"name" : "测试栏目"
									}, {
										"id" : "27",
										"parentId" : "1",
										"name" : "公共留言"
									}, {
										"id" : "6",
										"parentId" : "1",
										"name" : "质量检验"
									} ] */
						$.ajax({
						type: "get",
					    url: "${ctx}/cmsCategory/asyncList.do",     //ajax请求地址
					     success: function (data) {
					    	 var zNodes = JSON.parse(data);
					     $.fn.zTree.init($("#treeDemo"), setting, zNodes); //加载数据
					     },
					    error :function(data){
					    	alert(data);
					    }
					 });
					/* $.fn.zTree.init($("#treeDemo"), setting, zNodes); //加载数据   */ 
				});
	</script>
</body>
</html>